import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { stringFormat } from '/@/utils/common/compUtils';

enum Api {
  list = '/api-weixin/wx/account/list',
  save = '/api-weixin/wx/account/saveOrUpdate',
  edit = '/api-weixin/wx/account/saveOrUpdate',
  get = '/api-weixin/wx/account/{0}',
  delete = '/api-weixin/wx/account/delete',
  deleteBatch = '/api-weixin/wx/account/deleteBatch',
  sync = '/api-weixin/wx/account/sync',
}

/**
 * 查询帐户列表
 * @param params
 */
export const getAccountList = (params) => {
  return defHttp.get({ url: Api.list, params });
};

/**
 * 保存或者更新帐户
 * @param params
 */
export const saveOrUpdateAccount = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 查询帐户详情
 * @param params
 */
export const getAccountById = (params) => {
  const url = stringFormat(Api.get, params.id);
  return defHttp.get({ url: url, params });
};

/**
 * 删除帐户
 * @param params
 */
export const deleteAccount = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除帐户
 * @param params
 */
export const batchDeleteAccount = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 同步实例
 * @param params
 */
export const syncInstance = () => {
  return defHttp.delete({ url: Api.sync }, { joinParamsToUrl: true });
};
